package csu.web.credit_bank.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import csu.web.credit_bank.mapper.UserFavoritesMapper;
import csu.web.credit_bank.pojo.UserFavorite;
import csu.web.credit_bank.service.UserFavoritesService;
import csu.web.credit_bank.utils.UUIDManager;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserFavoritesServiceImpl extends ServiceImpl<UserFavoritesMapper, UserFavorite> implements UserFavoritesService {

    @Autowired
    private UUIDManager uuidManager;

    @Override
    public UserFavorite createFavorite(UserFavorite favorite) {
        String id = uuidManager.getUUID();
        if (id == null) {
            throw new RuntimeException("ID池已空，收藏失败");
        }
        favorite.setId(id);
        this.save(favorite);
        return favorite;
    }

    @Override
    public UserFavorite getFavorite(String userId, String itemId, Integer itemType) {
        return this.getOne(new QueryWrapper<UserFavorite>()
                .eq("user_id", userId)
                .eq("item_id", itemId)
                .eq("item_type", itemType));
    }

    @Override
    public UserFavorite updateFavorite(UserFavorite favorite) {
        this.updateById(favorite);
        return this.getById(favorite.getId());
    }

    @Override
    public boolean deleteFavorite(String id) {
        return this.removeById(id);
    }
}